import {
  Component,
  Injector,
  ViewEncapsulation,
  ViewChild,
  ElementRef,
} from "@angular/core";
import { AppComponentBase } from "@shared/common/app-component-base";
import {
  PipelineListDto,
  PipelineServiceProxy,
  PipelineNodeServiceProxy,
} from "@shared/service-proxies/service-proxies";
import { finalize } from "rxjs/operators";
import { appModuleAnimation } from "@shared/animations/routerTransition";
import * as moment from "moment";

@Component({
  selector: "rw21",
  templateUrl: "./rw21.component.html",
  encapsulation: ViewEncapsulation.None,
  animations: [appModuleAnimation()],
})
export class RW21ModalComponent extends AppComponentBase {
  saving = false;

  pipe: PipelineListDto = new PipelineListDto();

  zhiKongKaNo = '';

  newZhiKongKaNo = '';

  newAmount = 1;

  constructor(
    injector: Injector,
    private _taskServiceProxy: PipelineServiceProxy,
    private _pipelineNodeService:PipelineNodeServiceProxy
  ) {
    super(injector);
  }

  load(pipe: PipelineListDto): void {
    this.pipe = pipe;
    this._pipelineNodeService.getZhiKongKaNo(pipe.id).subscribe(result => {
      this.zhiKongKaNo = typeof result === 'object' ? '' : result;
    });

    this._pipelineNodeService
      .getCurrentActInputParamater(pipe.id)
      .subscribe(result => {
        if (result !== '') {
          result = result.replace(/[\{\}\"\"]/g, '');
          let zhi1 = result.split(',');
          this.newAmount = parseInt(zhi1[0].substring(7));
          this.newZhiKongKaNo = zhi1[1].substring(10);
        }
        console.log(result);
      });
  }

  save(): void {
    this.saving = true;
    if (
      this.newAmount == 0 ||
      this.newAmount >= this.pipe.amountPlanned
    ) {
      this.saving = false;
      this.notify.error("拆分数量不能大于投产数量");
    } else {
      // this._taskServiceProxy
      // .splitPipelineById(this.pipe.id, this.newAmount, this.newZhiKongKaNo)
      // .pipe(
      //   finalize(() => {
      //     this.saving = false;
      //   })
      // )
      // .subscribe(result => {
      //   this.notify.success('记录拆分数据成功');
      // });
      this._taskServiceProxy
        .splitPipeline(
          this.pipe.id,
          this.pipe.taskId,
          this.newZhiKongKaNo,
          this.newAmount
        )
        .pipe(
          finalize(() => {
            this.saving = false;
          })
        )
        .subscribe((result) => {
          this.notify.success("拆分成功");
        });
    }
  }
}

// import {
//   Component,
//   Injector,
//   ViewEncapsulation,
//   ViewChild,
//   ElementRef
// } from '@angular/core';
// import { AppComponentBase } from '@shared/common/app-component-base';
// import {
//   PipelineListDto,
//   ComboboxItemDto
// } from '@shared/service-proxies/service-proxies';
// import { PipelineNodeServiceProxy } from '@shared/service-proxies/service-proxies';
// import { finalize } from 'rxjs/operators';
// import { appModuleAnimation } from '@shared/animations/routerTransition';

// @Component({
//   selector: 'rw21',
//   templateUrl: './rw21.component.html',
//   encapsulation: ViewEncapsulation.None,
//   animations: [appModuleAnimation()]
// })
// export class RW21ModalComponent extends AppComponentBase {
//   saving = false;

//   pipe: PipelineListDto = new PipelineListDto();

//   zhiKongKaNo = '';

//   newZhiKongKaNo = '';

//   newAmount = 1;

//   constructor(
//     injector: Injector,
//     private _taskServiceProxy: PipelineNodeServiceProxy
//   ) {
//     super(injector);
//   }

//   load(pipe: PipelineListDto): void {
//     this.pipe = pipe;

//     this._taskServiceProxy.getZhiKongKaNo(pipe.id).subscribe(result => {
//       this.zhiKongKaNo = typeof result === 'object' ? '' : result;
//     });

//     this._taskServiceProxy
//       .getCurrentActInputParamater(pipe.id)
//       .subscribe(result => {
//         if (result !== '') {
//           result = result.replace(/[\{\}\"\"]/g, '');
//           let zhi1 = result.split(',');
//           this.newAmount = parseInt(zhi1[0].substring(7));
//           this.newZhiKongKaNo = zhi1[1].substring(10);
//         }
//         console.log(result);
//       });
//   }

//   save(): void {
//     this.saving = true;

//     this._taskServiceProxy
//       .splitPipelineById(this.pipe.id, this.newAmount, this.newZhiKongKaNo)
//       .pipe(
//         finalize(() => {
//           this.saving = false;
//         })
//       )
//       .subscribe(result => {
//         this.notify.success('记录拆分数据成功');
//       });
//   }
// }
